import time
import re
from urllib.request import urlopen
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from random import randint


def process_ip(cur_ip):
    pattern_nums = re.compile(r'\d+')
    ip_nums = re.findall(pattern_nums, cur_ip)
    ip_No3 = int(ip_nums[2])
    ip_No4 = int(ip_nums[3])
    if 3 < ip_No4 < 255:
        ip_No4 += 1
    elif ip_No4 == 255:
        ip_No4 = 4
        if ip_No3 == 94:
            ip_No3 = 95
        else:
            ip_No3 = 94
    ip_nums[2] = str(ip_No3)
    ip_nums[3] = str(ip_No4)
    ret = ip_nums[0]
    for i in range(1, 4):
        ret += '.' + ip_nums[i]
    return ret


Option = Options()
# Option.add_argument('--headless')  # 不额外将浏览器显示出来，界面整洁一点

# driver.get(url='https://gw.buaa.edu.cn/srun_portal_pc?ac_id=1&theme=buaa')
# time.sleep(5)
# A = driver.find_element(By.ID,'login')
# print(A)
#
# driver.quit()
driver = webdriver.Chrome(options=Option)
driver.get('http://192.168.3.1/html/index.html#/login')  # 登路由器管理页面
time.sleep(2)
driver.find_element(By.ID, "userpassword_ctrl").send_keys("buaae401")
driver.find_element(By.ID, "loginbtn").click()
time.sleep(2)
driver.find_element(By.ID, "internet").click()
time.sleep(2)
iptext = driver.find_element(By.ID, "wan_internet_ipaddress_ctrl")
cur_ip = iptext.get_attribute('value')
iptext.clear()
iptext.send_keys(process_ip(cur_ip))
driver.find_element(By.ID, "wan_submit_item_submitbutton").click()
time.sleep(20)
driver.quit()